package com.deyinet.bdcms.service.impl;


import com.deyinet.bdcms.core.dao.Dao;
import com.deyinet.bdcms.core.service.impl.BaseServiceImpl;
import com.deyinet.bdcms.dao.ISysrolerightDao;
import com.deyinet.bdcms.entity.Sysmodule;
import com.deyinet.bdcms.entity.Sysright;
import com.deyinet.bdcms.entity.Sysrole;
import com.deyinet.bdcms.entity.Sysroleright;
import com.deyinet.bdcms.service.ISysrolerightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Bacel on 2017/4/6.
 */
@Service
public class SysrolerightService extends BaseServiceImpl<Sysroleright> implements ISysrolerightService {

    @Autowired
    private ISysrolerightDao moduleDao;


    @Override
    protected Dao getDao() {
        return moduleDao;
    }

    @Override
    public void setRoleRight(int[] arr, int rid) {
        Sysroleright sysroleright = new Sysroleright();
        sysroleright.setRoleid(rid);
        List<Sysroleright> list = moduleDao.findByExampleEq(sysroleright);
        Serializable[] intArr = new Serializable[list.size()];
        for (int i = 0; i < list.size(); i++) {
            intArr[i] = list.get(i).getId();
        }
        if (intArr.length > 0) {
            moduleDao.batchDelete(intArr);
        }
        Timestamp now = new Timestamp(System.currentTimeMillis());
        Sysright right = new Sysright();
        right.setId(1);
        Sysrole sysrole=new Sysrole();
        sysrole.setId(rid);
        for (int i = 0; i < arr.length; i++) {
            Sysroleright newRighti = new Sysroleright();
            newRighti.setSysrightByRightid(right);
            newRighti.setSysroleByRoleid(sysrole);
            Sysmodule sysmodule=new Sysmodule();
            sysmodule.setId(arr[i]);
            newRighti.setSysmoduleByModuleid(sysmodule);
            newRighti.setCreatedate(now);
            moduleDao.save(newRighti);
        }
    }
}
